/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-6-21 下午4:23:34
 */
package com.absir.android.view;

import java.lang.reflect.Method;

import android.view.View;

import com.absir.android.view.annotation.InjectListener;
import com.absir.android.view.annotation.InjectMethod;
import com.absir.android.view.annotation.InjectView;
import com.absir.android.view.listener.Listener;
import com.absir.core.kernel.KernelLang.CauseRuntimeException;
import com.absir.core.kernel.KernelString;

/**
 * @author absir
 * 
 */
public class InjectViewMethod extends InjectViewAbstract {

	/** method */
	private Method method;

	/** injectMethod */
	private InjectMethod injectMethod;

	/**
	 * @param method
	 * @param injectMethod
	 */
	public InjectViewMethod(Method method, InjectMethod injectMethod) {
		this.method = method;
		this.injectMethod = injectMethod;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.android.view.InjectViewAbstract#injectExcutor(com.absir.android
	 * .view.InjectExcutorObject)
	 */
	@Override
	public void injectExcutor(InjectExcutorObject<Object> injectExcutorObject) {
		// TODO Auto-generated method stub
		for (InjectListener injectListener : injectMethod.value()) {
			View value = injectListener.value() == InjectView.ID_NONE ? injectExcutorObject.findViewById(injectListener.value(), "id", KernelString.subLastCapitalizeString(method.getName()))
					: injectExcutorObject.findViewById(injectListener.value(), null, null);
			if (value != null) {
				for (Class<? extends Listener> listenerClass : injectListener.listener()) {
					try {
						Listener listener = listenerClass.newInstance();
						listener.listener(value, injectExcutorObject.getTarget(null), method.getName());

					} catch (Exception e) {
						throw new CauseRuntimeException(e);
					}
				}
			}
		}
	}
}
